﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comissoes.Models;
using Comissoes.JQGridObject;

namespace Comissoes.Controllers
{
    public class NCMController : AppController
    {
        xcontEntities db = new xcontEntities();

        public ActionResult Index()
        {
            return View();
        }

        // Monta a lista de tipos de produtos
        public JsonResult List(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchField, String searchString, String searchOper)
        {
            try
            {
                var q = from c in db.NCM
                        select new
                        {
                            c.ID_NCM,
                            c.COD_NCM,
                            c.DESC_NCM,
                            ATIVO_NCM = c.ATIVO_NCM == "A" ? "Sim" : "Não"
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q, page, sidx, sord, searchField, searchString, searchOper, "ID_NCM", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        // Monta a lista de tipos de produtos
        public JsonResult UFRegimeTributarioList(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchField, String searchString, String searchOper, Int32 ID_NCM)
        {
            try
            {
                var q = from c in db.NCMS_UF_REGIME_TRIBUTARIO
                        where c.NCM.ID_NCM == ID_NCM
                        select new
                        {
                            c.ID_NURT,
                            ID_RGTR = c.REGIMES_TRIBUTARIOS.DESC_RGTR,
                            c.UFS.SIGLA_UF,
                            c.MVA_NURT,
                            c.REDUCAO_NURT,
                            c.ALIQUOTA_NURT
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q, page, sidx, sord, searchField, searchString, searchOper, "ID_NURT", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Details(Int32 id)
        {
            return View(db.NCM.FirstOrDefault(t => t.ID_NCM == id));
        }

        public ActionResult Edit(Int32? id)
        {
            return View(new NCMModel(id));
        }

        [HttpPost]
        public ActionResult Edit(Int32? id, FormCollection form)
        {
            List<String> erros = new List<string>();

            try
            {
                NCMModel ncm = new NCMModel(id);

                if (ncm.UpdateValidate(form).Any())
                    return Json(ncm.UpdateValidate(form), JsonRequestBehavior.AllowGet);

                else
                    ncm.Update(form);

                return Json(ncm.ncm.ID_NCM, JsonRequestBehavior.AllowGet);
            }
            catch(Exception ex)
            {
                erros.Add(ex.Message);
            }

            return Json(erros, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UFRegimeTributarioUpdate(Int32? ID_NCM, Int32? id, Int32? ID_RGTR, String SIGLA_UF, Decimal? MVA_NURT, Decimal? REDUCAO_NURT, Decimal? ALIQUOTA_NURT)
        {
            List<String> erros = new List<string>();

            if (!erros.Any())
            {
                try
                {
                    NCMS_UF_REGIME_TRIBUTARIO nurt = new NCMS_UF_REGIME_TRIBUTARIO();

                    if (id.HasValue)
                    {
                        nurt = db.NCMS_UF_REGIME_TRIBUTARIO.FirstOrDefault(t => t.ID_NURT == id);
                    }
                    else
                    {
                        db.AddToNCMS_UF_REGIME_TRIBUTARIO(nurt);
                    }

                    nurt.UFS = db.UFS.FirstOrDefault(t => t.SIGLA_UF == SIGLA_UF);
                    nurt.REGIMES_TRIBUTARIOS = db.REGIMES_TRIBUTARIOS.FirstOrDefault(t => t.ID_RGTR == ID_RGTR);
                    nurt.NCM = db.NCM.FirstOrDefault(t => t.ID_NCM == ID_NCM);
                    nurt.ALIQUOTA_NURT = ALIQUOTA_NURT;
                    nurt.MVA_NURT = MVA_NURT;
                    nurt.REDUCAO_NURT = REDUCAO_NURT;

                    db.SaveChanges();

                    return Json(true, JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    erros.Add(ex.Message);
                }
            }

            return Json(erros, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete()
        {
            NCMModel ncm = new NCMModel(Int32.Parse(Request["id"]));

            return Json(ncm.Delete(), JsonRequestBehavior.AllowGet);
        }
    }
}
